package com.cheer.ba.view.activity;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cheer.ba.R;
import com.cheer.ba.constant.ConstantTag;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.model.MyOrderModel;
import com.cheer.ba.presenter.impl.CardGroupPresenter;
import com.cheer.ba.utils.Currency;
import com.cheer.ba.utils.ImageUtils;
import com.cheer.ba.utils.Protect;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.view.base.BaseCommAty;

import butterknife.BindView;

/**
 * 订单详情
 * Created by Hq on 2017/7/5.
 */
public class OilOrderDetailAty extends BaseCommAty {

    @BindView(R.id.id_oil_company_txt)
    TextView mTxtCompanyName;
    //下单时间
    @BindView(R.id.id_oil_time_txt)
    TextView mTxtOrderTime;
    //
    @BindView(R.id.id_oil_pic)
    ImageView mImgPic;
    //标题
    @BindView(R.id.id_oil_title_txt)
    TextView mTxtOilTitle;
    //密度
    @BindView(R.id.id_oil_density_txt)
    TextView mTxtOilDensity;
    //油源
    @BindView(R.id.id_oil_type_txt)
    TextView mTxtOilType;
    //价格
    @BindView(R.id.id_oil_price_txt)
    TextView mTxtOilPrice;
    //购买数量
    @BindView(R.id.id_oil_num_txt)
    TextView mTxtOilBuyNum;
    //隐藏
    @BindView(R.id.id_oil_send_pay_linear)
    LinearLayout mLinear1;
    @BindView(R.id.id_order_btn_linear)
    LinearLayout mLinear2;
    //订单编号
    @BindView(R.id.id_oil_order_num_txt)
    TextView mTxtOrderNum;
    //收货人姓名
    @BindView(R.id.id_goods_name_txt)
    TextView mTxtGetGoodName;
    //收货人电话
    @BindView(R.id.id_goods_tel_txt)
    TextView mTxtGetGoodTel;
    //收货人地址
    @BindView(R.id.id_goods_address_txt)
    TextView mTxtGetGoodAddress;
    //隐藏
    @BindView(R.id.id_right_img)
    ImageView mImg1;
    //送货人姓名
    @BindView(R.id.id_send_good_name_txt)
    TextView mTxtSendName;
    //送货人电话
    @BindView(R.id.id_send_good_tel_txt)
    TextView mTxtSendTel;
    //送货人车牌号
    @BindView(R.id.id_send_good_car_num_txt)
    TextView mTxtSendCarNum;
    //送货人地址
    @BindView(R.id.id_send_good_address_txt)
    TextView mTxtSendAddress;

    //订单id
    private String mOrderId;

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_look_order_detail, null, false));
    }

    @Override
    public void initPresenter() {
        presenter = new CardGroupPresenter();
    }

    @Override
    public void initView() {
        mOrderId = getIntent().getStringExtra(IntentKey.General.KEY_ID);
        setTitleText("订单详情");
        mLinear1.setVisibility(View.GONE);
        mLinear2.setVisibility(View.GONE);
        mImg1.setVisibility(View.GONE);
    }

    @Override
    public void initData() {
        ((CardGroupPresenter) presenter).lookOrderDetails(mOrderId);
    }

    /**
     * 展示数据
     */
    public void showData(MyOrderModel model) {
        mTxtCompanyName.setText(model.orgNickName);
        mTxtOrderTime.setText("下单时间:" + StringUtils.getDetailStr(model.getCreateTime() + ""));
        ImageUtils.showRoundImg(aty, Protect.setPic(model.orgLogo), R.drawable.ic_pl_pic_bg, R.drawable.ic_pl_pic_bg, mImgPic);
        mTxtOilTitle.setText("柴油 0# " + (TextUtils.equals("1", model.oilModel) ? "普柴" : "国V"));
        mTxtOilDensity.setText(model.oilDensity);
        mTxtOilType.setText(model.oilSourceName);
        mTxtOilPrice.setText(Currency.returnDollar(model.tonPrice) + "/吨");
        mTxtOilBuyNum.setText("x " + model.stock);

        mTxtOrderNum.setText(model.getOrderId());
        mTxtGetGoodName.setText(model.recipientsName);
        mTxtGetGoodTel.setText(model.recipientsTel);
        mTxtGetGoodAddress.setText(model.recipientsLocation);

        mTxtSendName.setText(model.name);
        mTxtSendTel.setText(model.tel);
        mTxtSendCarNum.setText(model.plateNumber);
        mTxtSendAddress.setText(model.location);
    }

    @Override
    public <T> void updateViewWithTag(T t, String tag) {
        if (null != t) {
            if (TextUtils.equals(tag, ConstantTag.GET_ORDER_DETAILS)) {
                //成功
                final MyOrderModel model = (MyOrderModel) t;
                if (null != model) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            showData(model);
                        }
                    });
                } else {
                    dataStatus(3);
                }
            }
        }
    }
}
